package com.woniuxy.service.impl;

import com.woniuxy.dao.RoleDao;
import com.woniuxy.entity.Perm;
import com.woniuxy.entity.ResponseEntity;
import com.woniuxy.entity.Role;
import com.woniuxy.service.RoleService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.util.ArrayList;
import java.util.List;

/**
 * @Author：jojo
 * @Date：2022/3/8 17:21
 * @Description :TODO
 */
@Service
public class RoleServiceImpl implements RoleService {
    @Autowired(required = false)
    RoleDao roleDao;
    @Override
    public ResponseEntity addRole(Role role) {
        int num = roleDao.addRole(role);
        if(num>0){
            return ResponseEntity.success("添加成功");
        }else {
            return ResponseEntity.error("系统维护中....");
        }
    }
    @Override
    public ResponseEntity delet(int id) {
        int num = roleDao.delet(id);
        if(num>0){
            int num2 = roleDao.deletPerm(id);
            if (num2>0){
                return ResponseEntity.success("删除成功");
            }else{
                return ResponseEntity.error("系统维护中....");
            }
        }else {
            return ResponseEntity.error("系统维护中....");
        }
    }
    
    @Override
    public ResponseEntity update(Role role) {
        int num = roleDao.update(role);
        if(num>0){
            return ResponseEntity.success("修改成功");
        }else {
            return ResponseEntity.error("系统维护中....");
        }
    }

    @Override
    public ResponseEntity findAll() {
        List<Role> list = roleDao.findAll();
        if (list!=null){
            return ResponseEntity.success("查询成功",list);
        }else {
            return ResponseEntity.error("系统维护中....");
        }
    }

    @Override
    public ResponseEntity addPerm(int permid, int roleid) {
        int num = roleDao.findPerm(permid,roleid);
        System.out.println(num+"看着人");
        if (num > 0){
            return ResponseEntity.error(201,"已有该权限！");
        }
        int num2 = roleDao.addPerm(permid,roleid);
        if(num2 > 0){
            return ResponseEntity.success("添加成功！");
        }else {
            return ResponseEntity.error("系统维护中....");
        }
    }

    @Override
    public ResponseEntity findUserRole(int id) {
        List<Integer> ids = roleDao.findPermIds(id);
        List<Perm> list = new ArrayList();
        for (Integer permid:ids){
            Perm perm = roleDao.findP(permid);
            list.add(perm);
        }
        return ResponseEntity.success("success",list);
    }

    @Override
    public ResponseEntity findRole(int id) {
        String role = roleDao.findRole(id);
        if (role==null){
            role = "无";
        }
        System.out.println(role);
        return ResponseEntity.success("123",role);
    }
}
